package com.study.zzedu.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aliyun.player.alivcplayerexpand.bean.LongVideoBean;
import com.aliyun.solution.longvideo.activity.AlivcPlayerActivity;
import com.study.zzedu.R;

import com.study.zzedu.entity.OnLineLessonItem;
import com.study.zzedu.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 */

public class OnLineLessonAdapter extends RecyclerView.Adapter {

    private Context context;
    private List data;

    private final static int HEAD_COUNT = 1;

    private LongVideoBean bean = new LongVideoBean();
    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//创建日期转换对象HH:mm:ss为时分秒，年月日为yyyy-MM-dd

    @Override
    public int getItemViewType(int position) {
        return TYPE_CONTENT;
    }

    public OnLineLessonAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.layout_online_lesson_item, parent, false);
        return new ContentHolder(itemView);

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentHolder) { // 内容
            final OnLineLessonItem item = (OnLineLessonItem) data.get(position);

            ContentHolder myHolder = (ContentHolder) holder;
            myHolder.lessonName.setText(item.getTitle());
            myHolder.tv_course_name.setText(item.getTitle());
            myHolder.status.setText(item.getStatus());
            myHolder.tv_begintime.setText(item.getBeginDate() + "直播");

            try {
                Date dt_begin = df.parse(item.getBeginDate());//将字符串转换为date类型
                Date dt_end = df.parse(item.getEndDate());
                long currentTime = System.currentTimeMillis();
                if (currentTime > dt_end.getTime()) {
                    myHolder.tv_go_view.setVisibility(View.GONE);
                    myHolder.tv_review.setVisibility(View.VISIBLE);
                } else if (currentTime >= dt_begin.getTime() && currentTime <= dt_end.getTime()) {
                    myHolder.tv_go_view.setVisibility(View.VISIBLE);
                    myHolder.tv_review.setVisibility(View.GONE);
                } else {
                    myHolder.tv_go_view.setVisibility(View.GONE);
                    myHolder.tv_review.setVisibility(View.GONE);
                }
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            myHolder.tv_go_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(bean == null)
                        return;
                    bean.setTitle(item.getTitle());
                    getURL(item.getUrl());
//                    Intent intent = new Intent(context, StudyActivity.class);
//                    intent.putExtra("courseCode", item.getCourseCode());
//                    //intent.putExtra("chapterCode", item.get());
//                    intent.putExtra("lessonName", item.getLessonName());
//                    intent.putExtra("score", String.valueOf(item.getScore()));
//                    intent.putExtra("examType", item.getExamType());
//                    intent.putExtra("totalHour", item.getTotalHour());
//                    context.startActivity(intent);
                }
            });
            myHolder.tv_review.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    //内容
    public class ContentHolder extends RecyclerView.ViewHolder {

        private LinearLayout ll_item;
        private TextView lessonName, tv_curTerm, tv_go_view, tv_review, tv_begintime;
        private ImageView imgUrl;
        private TextView status,tv_course_name;

        public ContentHolder(View itemView) {
            super(itemView);
            ll_item = itemView.findViewById(R.id.ll_item);
            lessonName = itemView.findViewById(R.id.lesson_name);
            tv_course_name = itemView.findViewById(R.id.tv_course_name);
            imgUrl = itemView.findViewById(R.id.lesson_img);
            status = itemView.findViewById(R.id.tv_status);

            tv_curTerm = itemView.findViewById(R.id.tv_curTerm);
            tv_go_view = itemView.findViewById(R.id.tv_go_view);
            tv_review = itemView.findViewById(R.id.tv_review);
            tv_begintime = itemView.findViewById(R.id.tv_begintime);
        }
    }

    private void getURL(String URL){
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/liveClass/getLivePlayUrl",
                new String[]{"url"},
                new String[]{URL}, new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            JSONArray jArray = jObject.getJSONArray("data");

                            if (0 == jObject.getInt("code")) {
                               if(jArray.length() > 0){
                                   String url = jArray.get(0).toString();
                                   if(bean == null)
                                       return;
                                   bean.setSaveUrl(url);
                                   AlivcPlayerActivity.startAlivcPlayerActivity(context, bean);
//                                   Intent intent = new Intent(context, LoadingVideoActivity.class);
//                                   intent.putExtra("bean",(Serializable)bean);
//                                   context.startActivity(intent);
                               }
                            } else {

                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }
                    @Override
                    public void onSuccess(JSONObject obj) {
                        //resultListener.onSuccess(obj.toString());
                    }
                    @Override
                    public void onError(String msg) {
                        String kk = "";
                    }
                });
    }
}
